﻿using DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class BookDAL
    {
        SqlCommand cmd; //定义Command对象
        DataSet ds;//定义数据集对象
        public BookDAL()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure; //定义DB访问类型为存储过程
            ds = new DataSet();
        }
        public DataSet GetBookList()
        {
            cmd.CommandText = "GetBooks";
            cmd.Parameters.Clear(); //清除传参内容
            ds = DBhelper.QueryData(cmd); //调用通用数据访问类实现DataSet读取
            return ds;
        }

        public DataSet GetBookListByBookID(string inputString)
        {
            cmd.CommandText = "GetBookStateByBookID";
            cmd.Parameters.Clear();//清除传参内容

            cmd.Parameters.Add("@bookid", SqlDbType.VarChar, 20).Value = inputString;
            ds = DBhelper.QueryData(cmd);//调用通用数据库访问类实现DataSet读取

            return ds;
        }
        public DataSet GetBookListByBookName(string inputString)
        {
            cmd.CommandText = "GetBooksBybookname";
            cmd.Parameters.Clear();//清除传参内容

            cmd.Parameters.Add("@bookname", SqlDbType.NVarChar, 200).Value = inputString;
            ds = DBhelper.QueryData(cmd);//调用通用数据库访问类实现DataSet读取

            return ds;
        }
        public DataSet GetBookListByPublisher(string inputString)
        {
            cmd.CommandText = "GetBooksBypublisher";
            cmd.Parameters.Clear();//清除传参内容

            cmd.Parameters.Add("@publisher", SqlDbType.NVarChar, 50).Value = inputString;
            ds = DBhelper.QueryData(cmd);//调用通用数据库访问类实现DataSet读取

            return ds;
        }

        public DataSet GetBookListByAuthor(string inputString)
        {
            cmd.CommandText = "GetBooksByAuthor";
            cmd.Parameters.Clear();//清除传参内容

            cmd.Parameters.Add("@author", SqlDbType.NVarChar, 50).Value = inputString;
            ds = DBhelper.QueryData(cmd);//调用通用数据库访问类实现DataSet读取

            return ds;
        }
        public object EditBoek(BookVM vm, string operation)
        {
            if (operation == "I")
            cmd.CommandText = "InsertBook";//当传入标识为I，进行书籍新增
            if (operation == "U")
                cmd.CommandText = "UpdateBook";//当传入标识为U，进行书籍修改
            cmd.Parameters.Clear();
            cmd.Parameters.Add(" @BookID",SqlDbType.VarChar, 20).Value = vm.BookID;
            cmd.Parameters.Add(" @BookName",SqlDbType. NVarChar,200). Value = vm. BookName ;
            cmd.Parameters.Add( "@Author",SqlDbType.NVarChar, 50).Value = vm.Author;
            cmd.Parameters.Add(" @Publisher", SqlDbType.NVarChar, 50).Value = vm.Publisher;
            cmd.Parameters.Add(" @ISBN",SqlDbType.Char, 20).Value = vm.ISBN;
            cmd.Parameters.Add(" @Prices",SqlDbType.Money).Value=vm.Price;
            cmd.Parameters.Add(" @WordCount", SqlDbType.Int).Value = vm.Wordcont;
            cmd.Parameters.Add(" @PageCount", SqlDbType.Int).Value = vm.PageCount;
            cmd.Parameters.Add(" @ClassID",SqlDbType. VarChar, 10). Value = vm. ClassID;
            cmd.Parameters.Add(" @PublishDate",SqlDbType. DateTime). Value = vm. PublishDate;
            cmd.Parameters.Add(" @BookVersion",SqlDbType. NVarChar, 40). Value = vm. BookVersion; 
        }
    }
}
